package com.travelms.service.adminuserpage;

import com.travelms.dao.DBFind;
import com.travelms.pojo.Hotel;
import com.travelms.pojo.Lvyou;
import com.travelms.pojo.Train;
import com.travelms.util.DataEffect;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class AlterGoodsInfo {

    public AlterGoodsInfo() throws SQLException, IOException, InstantiationException, IllegalAccessException {
        Stack<Integer> stack = new Stack<>();
        Scanner sc = new Scanner(System.in);
        int current = 0;
        stack.push(current);

        while (true) {

            if (current == 0) {
                System.out.println("1.修改正在售卖的车票信息");
                System.out.println("2.修改正在售卖的酒店信息");
                System.out.println("3.修改正在预约的旅游信息");

            }
            while (true) {
                System.out.println("亲输入:");
                String s = sc.nextLine();
                if (!DataEffect.isNumber(s)) {//输入效验
                    System.out.println("输入错误");
                } else if (!(s.compareTo("0") > 0 && s.compareTo("6") < 0)) {
                    System.out.println("输入错误");
                } else {
                    current = Integer.parseInt(s);//记录输入的层级
                    stack.push(current);//压入栈
                    break;
                }
            }
            if (current == 4) {
                //修改正在售卖的车票信息
                //为3则退出
                break;
            } else if (current == 1) {
                new modifyTrainInfo(new Train());
            }
            else if (current == 2) {
                new modifyTrainInfo(new Hotel());
            }
            else if (current == 3) {
                new modifyTrainInfo(new Lvyou());
            }
        }
    }
}


class modifyTrainInfo {
    private DBFind dbFind = new DBFind();
    Scanner sc = new Scanner(System.in);
    public <T>modifyTrainInfo(T info) throws SQLException, IOException, InstantiationException, IllegalAccessException {
        Stack<Integer> stack = new Stack<>();
        Scanner sc = new Scanner(System.in);
        int current = 0;
        stack.push(current);

        while (true) {

            if (current == 0) {
                System.out.println("1.修改价格");
                System.out.println("2.修改剩余数量");
                System.out.println("3.下架商品");
//                System.out.println("4.上架商品");
                System.out.println("5.退出");

            }
            while (true) {
                System.out.println("亲输入:");
                String s = sc.nextLine();
                if (!DataEffect.isNumber(s)) {//输入效验
                    System.out.println("输入错误");
                } else if (!(s.compareTo("0") > 0 && s.compareTo("6") < 0)) {
                    System.out.println("输入错误");
                } else {
                    current = Integer.parseInt(s);//记录输入的层级
                    stack.push(current);//压入栈
                    break;
                }
            }
            if (current == 5) {
                //为3则退出
                break;
            } else if (current == 1) {

                System.out.println("正常售卖信息如下:");

                try {
                    ArrayList infoList = dbFind.getAllInfo(info);
                    for (Object o : infoList) {
                        System.out.println(o);
                    }

                } catch (InstantiationException e) {
                    throw new RuntimeException(e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }

                while (true){
                    System.out.println("输入你要修改的价格的id:");
                    String id = sc.nextLine();
                    System.out.println("输入你要修改的价格:");
                    String price = sc.nextLine();
                    if (id.isEmpty()){
                        System.out.println("输出错误");
                        continue;
                    }

                    if (dbFind.modifyPrice(info,price,"money",id)){
                        System.out.println("修改成功");
                        try {
                            System.out.println("修改后的信息如下");
                            ArrayList infoList = dbFind.getAllInfo(info);
                            for (Object o : infoList) {
                                System.out.println(o);
                            }
                            System.out.println("按任意键退出");
                            String q = sc.nextLine();
                        } catch (InstantiationException e) {
                            throw new RuntimeException(e);
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    }else {
                        System.out.println("没有该id");
                        continue;
                    }



                }

                stack.pop();
                current  = stack.peek();
            }else if (current == 2){
                System.out.println("正常售卖信息如下:");

                try {
                    ArrayList infoList = dbFind.getAllInfo(info);
                    for (Object o : infoList) {
                        System.out.println(o);
                    }

                } catch (InstantiationException e) {
                    throw new RuntimeException(e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                while (true){
                System.out.println("输入你要修改的数量的id:");
                String id = sc.nextLine();
                System.out.println("输入你要修改的数量:");
                String price = sc.nextLine();
                if (id.isEmpty()){
                    System.out.println("输出错误");
                    continue;
                }

                if (dbFind.modifyPrice(info,price,"reminder",id)){
                    System.out.println("修改成功");
                    try {
                        System.out.println("修改后的信息如下");
                        ArrayList infoList = dbFind.getAllInfo(info);
                        for (Object o : infoList) {
                            System.out.println(o);
                        }
                        System.out.println("按任意键退出");
                        String q = sc.nextLine();
                    } catch (InstantiationException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                }else {
                    System.out.println("没有该id");
                    continue;
                }
            }
                stack.pop();
                current  = stack.peek();

        } else if (current == 3) {
                //下架商品
                System.out.println("当前信息如下");
                ArrayList infoList = dbFind.getAllInfo(info);
                for (Object o : infoList) {
                    System.out.println(o);
                }
                while (true) {
                    System.out.println("请选择要下架的商品id");
                    String id = sc.nextLine();
                    if (id.isEmpty()){
                        System.out.println("输出错误");
                        continue;
                    }
                    if (dbFind.removeGoods(info,id)){
                        System.out.println("下架成功");
                        System.out.println("修改后的信息如下");
                        infoList = dbFind.getAllInfo(info);
                        for (Object o : infoList) {
                            System.out.println(o);
                        }
                        System.out.println("按任意键退出");
                        String q = sc.nextLine();
                        break;
                    }else {
                        System.out.println("修改失败");
                        break;
                    }
                }
                stack.pop();
                current  = stack.peek();
            }
        }

    }
}